﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System8Equations
{
    public class UniformS8Variable : S8Variable
    {
        public double MinValue { get; set; }

        public double MaxValue { get; set; }

        private double OldValue;

        public UniformS8Variable(double centralValue)
        {
            OldValue = centralValue;
            MaxValue = OldValue * 1.1;
            MinValue = OldValue * 0.9;
        }

        /// <summary>
        /// Keep the random number generator around
        /// </summary>
        private Random _rnd = new Random();

        /// <summary>
        /// Gets the value we are going to show
        /// </summary>
        /// <returns></returns>
        protected override double GetValue()
        {
            return _rnd.NextDouble() * (MaxValue-MinValue) + MinValue;
        }

        /// <summary>
        /// Return a user-readable version of what this is.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string result = base.ToString();
            result += " Norm(" + MinValue.ToString("F3") + "-" + MaxValue.ToString("F3") + ")";
            return result;
        }

        /// <summary>
        /// New central value. We use basic % to re-calc
        /// </summary>
        /// <param name="newCentralValue"></param>
        public override void Update(double newCentralValue)
        {
            double oldMinPercent = 1 + (MinValue - OldValue) / OldValue;
            double oldMaxPercent = 1 + (MaxValue - OldValue) / OldValue;

            OldValue = newCentralValue;

            MinValue = OldValue * oldMinPercent;
            MaxValue = OldValue * oldMaxPercent;

            ///
            /// Notivy anyone that cares
            /// 

            OnValueChanged();
        }
    }
}
